package persistance;

import java.util.List;

import javax.annotation.Resource;

import model.SimpleUser;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


/**
 * 首先在mysql中建表
 *  create table springuser(username varchar(20) ,age int);
 *  
 *  查询时候必须要注意！！ BeanPropertyRowMapper负责组装
 * @author lcx
 *
 */
@Repository("userdao")
public class UserDAOImpl implements UserDAO {

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate = null;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void insert(SimpleUser user) {
		String sql="insert into springuser values('"+user.getUserName()+"',"+user.getAge()+")";
		jdbcTemplate.execute(sql);
		
	}
	
	public void select() {
		String sql="select username,age from springuser ";
		List<SimpleUser> list=(List<SimpleUser>)jdbcTemplate.query(sql,new BeanPropertyRowMapper(SimpleUser.class));
		for(SimpleUser user:list)
			user.info();
	}


}
